package com.attendance.www.hr.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.RequestBean;
import com.attendance.www.hr.beans.RetroactiveBean;
import com.attendance.www.hr.beans.RetroactivePageBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.entitys.Attendance;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IRequestService;
import com.attendance.www.hr.service.IRetroactiveService;
import com.attendance.www.hr.service.IRetroactiveTimesService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 考勤补签
 * @author 张鑫
 * 2017-09-29
 *
 */
@Controller
@RequestMapping("/retroactive")
public class RetroactiveController {
	
	@Resource
	private IRetroactiveService retroactiveService;
	@Resource
	private IRequestService requestService;
	@Resource
	private IRetroactiveTimesService retroactiveTimesService;
	
	/**
	 * 增加一条补签信息
	 * @param retroactiveBean
	 * @return
	 */
	@RequestMapping(value = "/saveRetroactive.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody 
	public Object saveRetroactive(HttpServletRequest request,@RequestBody RetroactiveBean retroactiveBean){
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		
		//申请开关
		boolean applicationType=Boolean.parseBoolean(AppConstant.APPLICATION_TYPE);
		if(applicationType) {
			throw new ResultFaultException("4_00027");
		}
		
		if (StringUtils.isBlank(retroactiveBean.getStartTime()) && StringUtils.isBlank(retroactiveBean.getEndTime())) {
			throw new ResultFaultException("1_00001");
		}
		if (StringUtils.isBlank(retroactiveBean.getEndTime())) {
			retroactiveBean.setEndTime(null);
		}
		if (StringUtils.isBlank(retroactiveBean.getStartTime())) {
			retroactiveBean.setStartTime(null);
		}
		if(retroactiveBean!=null){
			if (retroactiveBean.getAttendanceId() == null) {
				throw new ResultFaultException("0_00001");
			}
			Attendance attendance=retroactiveService.getAttendanceByAttendanceId(retroactiveBean.getAttendanceId());
			if(attendance!=null){
				RequestBean requestBean=new RequestBean();
				requestBean.setStartDate(attendance.getAttendanceDate());
				if(requestService.verifySaveRequestClosed(requestBean)){
					throw new ResultFaultException("4_00003");
				}
			}
			if (retroactiveService.saveRetroactive(retroactiveBean,userEmployee.getEmployeeId())) {
				return ResultMap.success();
			}
		}
		throw new ResultFaultException("0_00007");
		
	}
	
	/**
	 * 获取用户考勤补签信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getRetroactiveList.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getRetroactiveList(HttpServletRequest request,@RequestBody RetroactiveBean retroactiveBean){
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId = userEmployee.getEmployeeId();
		Date attendanceDateStart = retroactiveBean.getAttendanceDateStart();
		Date attendanceDateEnd=retroactiveBean.getAttendanceDateEnd();
		if (attendanceDateStart == null||attendanceDateEnd== null) {
			throw new ResultFaultException("0_00001");
		}
		Integer pageNo = retroactiveBean.getPageNo();
		Integer pageCount = retroactiveBean.getPageCount();
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		RetroactivePageBean retroactivePageBean=new RetroactivePageBean();
		retroactivePageBean=retroactiveService.getRetroactiveList(employeeId, attendanceDateStart, attendanceDateEnd, startNum, pageCount);
		return ResultMap.success(retroactivePageBean);
	}
	
	/**
	 * 获取补签次数
	 * @param request
	 * @param retroactiveBean
	 * @return
	 */
	@RequestMapping(value = "/getRetroactiveNum.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getRetroactiveNum(HttpServletRequest request,@RequestBody RetroactiveBean retroactiveBean){
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Date attendanceDateStart=retroactiveBean.getAttendanceDateStart();
		Date attendanceDateEnd=retroactiveBean.getAttendanceDateEnd();
		if (attendanceDateStart == null||attendanceDateEnd== null) {
			throw new ResultFaultException("0_00001");
		}
		Integer retroactiveNum=retroactiveService.getRetroactiveNum(userEmployee.getEmployeeId(),attendanceDateStart, attendanceDateEnd);
		return ResultMap.success(retroactiveNum);
	}
	
	/**
	 * HR获取用户考勤补签信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getRetroactiveListByHR.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getRetroactiveListByHR(HttpServletRequest request,@RequestBody RetroactiveBean retroactiveBean){
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		//仅查看HR分配的公司
		if (employeeId != null) {
			retroactiveBean=retroactiveService.getRetroactiveBeanByHR(retroactiveBean, employeeId);
		}
		Date attendanceDateStart = retroactiveBean.getAttendanceDateStart();
		Date attendanceDateEnd=retroactiveBean.getAttendanceDateEnd();
		if (attendanceDateStart == null||attendanceDateEnd== null) {
			throw new ResultFaultException("0_00001");
		}
		Integer pageNo = retroactiveBean.getPageNo();
		Integer pageCount = retroactiveBean.getPageCount();
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		retroactiveBean.setStartNum(startNum);
		RetroactivePageBean retroactivePageBean=new RetroactivePageBean();
		retroactivePageBean=retroactiveService.getRetroactiveListByHR(retroactiveBean);
		return ResultMap.success(retroactivePageBean);
	}
	
	/**
	 * 一键补签
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveOnceRetroactive.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getOnceRetroactive(HttpServletRequest request,@RequestBody RetroactiveBean retroactiveBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		
		retroactiveBean.setEmployeeId(userEmployee.getEmployeeId());
		if (StringUtils.isBlank(retroactiveBean.getStartTime()) 
				&& StringUtils.isBlank(retroactiveBean.getEndTime())) {
			throw new ResultFaultException("1_00001");
		}
		
		//申请开关
		boolean applicationType=Boolean.parseBoolean(AppConstant.APPLICATION_TYPE);
		if(applicationType) {
			throw new ResultFaultException("4_00027");
		}
		
		//验证封账
		RequestBean requestBean=new RequestBean();
		requestBean.setStartDate(retroactiveBean.getAttendanceDate());
		if(requestService.verifySaveRequestClosed(requestBean)){
			throw new ResultFaultException("4_00003");
		}
		if (retroactiveService.saveOnceRetroactive(retroactiveBean)) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00007");
	}
	
	/**
	 * 跨天补签
	 * @param request
	 * @param retroactiveBean
	 * @return
	 */
	@RequestMapping(value = "/saveAcrossRetroactive.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object saveAcrossRetroactive(HttpServletRequest request,@RequestBody RetroactiveBean retroactiveBean) {
		if ((StringUtils.isBlank(retroactiveBean.getStartTime()) && StringUtils.isBlank(retroactiveBean.getEndTime()))
				||retroactiveBean.getFirstDate()==null
				||retroactiveBean.getRoleId()==null) {
			throw new ResultFaultException("1_00001");
		}
		
		//获取登陆用户数据 
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		
		//申请开关
		boolean applicationType=Boolean.parseBoolean(AppConstant.APPLICATION_TYPE);
		if(applicationType) {
			throw new ResultFaultException("4_00027");
		}

		retroactiveBean.setEmployeeId(userEmployee.getEmployeeId());
		//验证封账
		RequestBean requestBean=new RequestBean();
		requestBean.setStartDate(retroactiveBean.getFirstDate());
		if(requestService.verifySaveRequestClosed(requestBean)){
			throw new ResultFaultException("4_00003");
		}
		if (retroactiveService.saveAcrossRetroactive(retroactiveBean)) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00007");
	}
	
	/**
	 * 获取某天能撤回的补签申请List
	 * @param request
	 * @param retroactiveBean
	 * @return
	 */
	@RequestMapping(value = "/getRetroactives.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getRetroactives(HttpServletRequest request,@RequestBody RetroactiveBean retroactiveBean){
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId = userEmployee.getEmployeeId();
		retroactiveBean.setEmployeeId(employeeId);
		return ResultMap.success(retroactiveService.getRetroactives(retroactiveBean));
	}
}
